#coding=utf-8

import json
import os 
import shutil

# 将一行配置文件分割
def splitConfLine(line):
    arr = []
    ss = ''
    s = ''
    a = []
    for i, val in enumerate(line):
        s = val
        if s ==  '"':
            a.append(s.replace("\n",""))
        elif s == ',' :
            if len(a) == 1:
                ss += s ;
                continue ;
            elif len(a) == 2:
                a = []
            arr.append(ss.replace("\n",""))
            ss = ''
        else :
            ss += s
    if ss != '':
        arr.append(ss.replace("\n",""))
    return arr

#将字段名转为驼峰写法
def parseProperites(s):
    index = s.find("_")
    if index == -1:
        return s.lower()
    string_list = str(s).split("_")  # 将字符串转化为list
    first = string_list[0].lower()
    others = string_list[1:]
    others_capital = [word.capitalize() for word in others]  # str.capitalize():将字符串的首字母转化为大写
    others_capital[0:0] = [first]
    hump_string = ''.join(others_capital)  # 将list组合成为字符串，中间无连接符。
    return hump_string

#读取json文件
def loadJson(filePath):
    with open(filePath, encoding='utf-8') as json_file:
        data = json.load(json_file)
        return data

#通过关键字获得词典名字
def getDicNameByKeys(keys,returnArr):
    name = "dic";
    for(i,val) in enumerate(keys):
        name += "_" + val;
    name = parseProperites(name)
    if returnArr:
        name += "Arr"
    return name

#通过关键字获得数组词典名字
def getConfListNameByKeys(keys,returnArr):
    name = "confList";
    for(i,val) in enumerate(keys):
        name += "_" + val;
    name = parseProperites(name)
    if returnArr:
        name += "Arr"
    return name

def fileReadLine(fileReader):
   return fileReader.readline().replace("\n","")

def getTsParamType(typeStr):
    if typeStr == "int" or typeStr == "long" or typeStr == "short" or \
            typeStr == "float" or typeStr == "double" or typeStr == "number" or typeStr == "smallint" :
        return "number"
    else:
        return "string"

#初始化一个文件所在的目录 
def initFileDir(path):
    dirPath = os.path.dirname(path) 
    if not os.path.exists(dirPath):
        os.makedirs(dirPath)
        
#清理一个目录，实际上是删掉再创建
def clearDir(dirPath):
    if os.path.exists(dirPath):  
        shutil.rmtree(dirPath)
        os.makedirs(dirPath)
